package sg.edu.nus.iss.vmcs.customer;

import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionListener;

import sg.edu.nus.iss.vmcs.util.LabelledDisplay;

public class CoinInputBox extends Panel
{

    /**
	 * 
	 */
    private static final long serialVersionUID = 1L;

    private Label enterCoinLabel;
    @SuppressWarnings("unused")
    private Button _5cButton;
    @SuppressWarnings("unused")
    private Button _10cButton;
    @SuppressWarnings("unused")
    private Button _20cButton;
    @SuppressWarnings("unused")
    private Button _50cButton;
    @SuppressWarnings("unused")
    private Button _1$cButton;
    @SuppressWarnings("unused")
    private Button invalidButton;
    private Label coinStatusLabel;
    private LabelledDisplay totalMoneyInsertedLabel;

    private static final int NUM_BUTTONS = 6;
    private Button buttons[] = new Button[NUM_BUTTONS];
    private Font buttonFont = new Font("Helvetica", Font.BOLD, 14);
    private Font titleFont = new Font("Helvetica", Font.BOLD, 15);

    public static final int _5C = 0;
    public static final int _10C = 1;
    public static final int _20C = 2;
    public static final int _50C = 3;
    public static final int _1$ = 4;
    public static final int INVALID = 5;

    private int totalMoneyInserted = 0;
    Panel coinStatusPanel;

    public CoinInputBox(CustomerPanelPresenter presenter)
    {
	GridBagLayout gridBag = new GridBagLayout();
	this.setLayout(gridBag);

	GridBagConstraints c = new GridBagConstraints();
	c.fill = GridBagConstraints.BOTH;
	c.gridwidth = GridBagConstraints.REMAINDER;
	c.weightx = 1.0;

	enterCoinLabel = new Label("Enter Coins Here");
	enterCoinLabel.setFont(titleFont);
	enterCoinLabel.setAlignment(Label.LEFT);
	gridBag.setConstraints(enterCoinLabel, c);
	this.add(this.enterCoinLabel);

	Panel coinButtonPanel = new Panel();
	coinButtonPanel.setLayout(new GridLayout());
	addButton(_5C, "5C", coinButtonPanel, presenter.getCoinInputListener());
	addButton(_10C, "10C", coinButtonPanel, presenter
		.getCoinInputListener());
	addButton(_20C, "20C", coinButtonPanel, presenter
		.getCoinInputListener());
	addButton(_50C, "50C", coinButtonPanel, presenter
		.getCoinInputListener());
	addButton(_1$, "$1", coinButtonPanel, presenter.getCoinInputListener());
	addButton(INVALID, "Invalid", coinButtonPanel, presenter
		.getCoinInputListener());

	c.weightx = 0.0;
	gridBag.setConstraints(coinButtonPanel, c);
	this.add(coinButtonPanel);

	coinStatusPanel = new Panel(new FlowLayout(FlowLayout.CENTER));
	this.coinStatusLabel = new Label("Invalid Coin");
	coinStatusLabel.setVisible(true);
	this.coinStatusLabel.setBackground(Color.white);
	this.coinStatusLabel.setForeground(Color.white);
	this.coinStatusLabel.setAlignment(Label.CENTER);
	coinStatusPanel.add(coinStatusLabel);
	gridBag.setConstraints(coinStatusPanel, c);
	add(coinStatusPanel);

	this.totalMoneyInsertedLabel = new LabelledDisplay(
		"Total Money Inserted:", 20, LabelledDisplay.FLOW);
	this.totalMoneyInsertedLabel.setEditable(false);
	gridBag.setConstraints(totalMoneyInsertedLabel, c);
	add(totalMoneyInsertedLabel);

    }

    public void showCoinStatus()
    {
	this.coinStatusLabel.setBackground(Color.black);
	this.coinStatusPanel.setVisible(true);
    }

    public void hideCoinStatus()
    {
	this.coinStatusPanel.setVisible(false);
    }

    public void active(boolean status)
    {
	for (int i = 0; i < CoinInputBox.NUM_BUTTONS; i++)
	{
	    buttons[i].setEnabled(status);
	}
    }

    private void addButton(int id, String label, Panel panel,
	    ActionListener listener)
    {
	Button b = new Button(label);
	b.setBackground(Color.white);
	b.setForeground(Color.blue);
	b.setFont(buttonFont);
	buttons[id] = b;
	b.addActionListener(listener);
	panel.add(b);
    }

    public void setTotalMoneyInserted(int totalMoneyInserted)
    {
	this.totalMoneyInserted += totalMoneyInserted;
	if (this.totalMoneyInserted < 100)
	    totalMoneyInsertedLabel.setValue(String
		    .valueOf(this.totalMoneyInserted)
		    + "C");
	else
	    totalMoneyInsertedLabel.setValue("$"
		    + String.valueOf(this.totalMoneyInserted / 100.0));
    }

    public void resetTotalMoney(int totalMoneyInserted)
    {
	this.totalMoneyInserted = 0;
	totalMoneyInsertedLabel.setValue("");
    }

    public int getTotalMoneyInserted()
    {
	return totalMoneyInserted;
    }

    public void reset()
    {
	active(false);
	hideCoinStatus();
	totalMoneyInsertedLabel.setValue("");
	totalMoneyInserted = 0;
    }
}
